﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SmartCity.Shared;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace CommonManagement
{
    /// <summary>
    /// 城市应用服务类
    /// </summary>
    public class CityAppService : ApplicationService, ICityAppService
    {
        private readonly IRepository<City, int> _cityRepository;

        public CityAppService(
            IRepository<City, int> cityRepository)
        {
            _cityRepository = cityRepository;
        }

        // 列表
        public async Task<ResultResponse<IList<CityDto>>> ListAsync(string provinceName, string name = null)
        {
            // 查询
            var query = _cityRepository
                .Where(m => m.Province.Name.Equals(provinceName))
                .WhereIf(!string.IsNullOrWhiteSpace(name), m => m.Name.Contains(name))
                .Include(m => m.Province);
            var list = await query.ToListAsync();
            // 返回结果
            var dtos = ObjectMapper.Map<List<City>, List<CityDto>>(list);
            return ResultResponse<IList<CityDto>>.Successful(dtos);
        }
    }
}